<template>
  <el-dialog
    :append-to-body="true"
    :modal-append-to-body="false"
    :visible.sync="dialogVisible"
    :title="title"
    width="500px"
    @callBack="onCallBack"
    @close="onClose"
  >
    <el-form
      ref="ruleForm"
      :model="form"
      :rules="rules"
      label-width="100px"
      label-position="left"
    >
      <el-form-item label="逻辑连接符" prop="type">
        <el-select
          v-model="form.type"
          placeholder="请选择逻辑连接符"
          style="width: 100%"
        >
          <el-option
            v-for="item in type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="源信息集" prop="sourceTableId">
        <el-select
          v-model="form.sourceTableId"
          placeholder="请选择源信息集"
          style="width: 100%"
          @change="onSourceTableChange"
        >
          <el-option
            v-for="item in tableOptions"
            :key="item.tableId"
            :label="item.tableComment"
            :value="item.tableId"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="源列" prop="sourceColId">
        <el-select
          v-model="form.sourceColId"
          placeholder="请选择源列"
          style="width: 100%"
        >
          <el-option
            v-for="item in sourceColOptions"
            :key="item.name"
            :label="item.comment"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="连接关系" prop="related">
        <el-select
          v-model="form.related"
          placeholder="请选择连接关系"
          style="width: 100%"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="目标信息集" prop="targetTableId">
        <el-select
          v-model="form.targetTableId"
          placeholder="请选择目标信息集"
          style="width: 100%"
          @change="onTargetTableChange"
        >
          <el-option
            v-for="item in tableOptions"
            :key="item.tableId"
            :label="item.tableComment"
            :value="item.tableId"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="目标列" prop="targetColId">
        <el-select
          v-model="form.targetColId"
          placeholder="请选择源列"
          style="width: 100%"
        >
          <el-option
            v-for="item in targetColOptions"
            :key="item.name"
            :label="item.comment"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="onClose">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { save } from "@/api/sysQueryRelevanceCondition";
import { getQueryTables } from "@/api/sysQueryTable";
import { getColByTableId } from "@/api/sysColumn";

export default {
  data() {
    return {
      dialogVisible: false,
      title: "添加",
      tableOptions: [],
      sourceColOptions: [],
      targetColOptions: [],
      type: [
        {
          value: 1,
          label: "AND",
        },
        {
          value: 2,
          label: "OR",
        },
      ],
      options: [
        {
          value: 1,
          label: "左链接",
        },
        {
          value: 2,
          label: "内链接",
        },
        {
          value: 3,
          label: "右链接",
        },
      ],
      form: {
        id: "",
        mainId: "",
        sourceTableId: "",
        sourceColId: "",
        targetTableId: "",
        targetColId: "",
        related: "",
      },
      rules: {
        type: [
          {
            required: true,
            message: "请选择逻辑关系",
            trigger: "blur",
          },
        ],
        sourceTableId: [
          {
            required: true,
            message: "请选择信息集",
            trigger: "blur",
          },
        ],
        sourceColId: [
          {
            required: true,
            message: "请选择展示列",
            trigger: "blur",
          },
        ],
        sourceColId: [
          {
            required: true,
            message: "请选择展示列",
            trigger: "blur",
          },
        ],
        related: [
          {
            required: true,
            message: "请选择连接关系",
            trigger: "blur",
          },
        ],
        targetColId: [
          {
            required: true,
            message: "请选择展示列",
            trigger: "blur",
          },
        ],
        showName: [
          {
            required: true,
            message: "请输入展示名称",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    init(mainId) {
      getQueryTables(mainId).then((res) => {
        if (res && res.code === 20000) {
          this.tableOptions = res.data;
        }
      });
    },
    onShow(mainId) {
      this.init(mainId);
      this.form.mainId = mainId;
      this.dialogVisible = true;
    },
    onClose() {
      this.$refs["ruleForm"].resetFields();
      this.form = this.$options.data().form;
      this.dialogVisible = false;
    },
    onSourceTableChange(tableId) {
      getColByTableId(tableId).then((res) => {
        if (res && res.code === 20000) {
          this.sourceColOptions = res.data;
        }
      });
    },
    onTargetTableChange(tableId) {
      getColByTableId(tableId).then((res) => {
        if (res && res.code === 20000) {
          this.targetColOptions = res.data;
        }
      });
    },
    onSubmit() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          save(this.form)
            .then((res) => {
              if (res && res.code === 20000) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                });
                this.dialogVisible = false;
                this.onCallBack();
              }
            })
            .catch((e) => {});
        } else {
          return false;
        }
      });
    },
    onCallBack() {
      this.$emit("onCallBack");
    },
  },
};
</script>

<style scoped>
.el-form {
  padding-left: 60px;
  padding-right: 60px;
}
</style>
